#include "1mysql.h"
#include "select.h"
MYSQL * Sconnect()//连接数据库
{
   static MYSQL my_sql;
    //0.初始化核心结构体
    mysql_init(&my_sql);
    //1.连接数据库
    //指定使用"hello"数据库
    MYSQL*p =  mysql_real_connect(&my_sql,HOSTNAME,USERNAME,PASSWD,DATABASENAME,0,NULL,0);
     if(p == NULL)
    {
        // perror("mysql_real_connect");
        printf("mysql_error:%s\n",mysql_error(&my_sql));
        return NULL;
    }
    printf("数据库连接成功\n");
    return &my_sql;
}
 
void print_all_data(MYSQL *mysql,Msg *msg)
{
    char buf[256];
    int ret = 0;
    memset(buf,0,sizeof(buf));
    sprintf(buf,"SELECT * FROM %s",TABLENAME);
    // printf("buf:%s\n",buf);
    ret = mysql_real_query(mysql,buf,strlen(buf));
    if(ret != 0)
    {
        printf("mysql_error:%s\n",mysql_error(mysql));
        return;
    }
    // printf("查询数据成功\n");
    //获取结果集
    MYSQL_RES *pres = mysql_store_result(mysql);
    if(pres == NULL)
    {
        printf("mysql_error:%s\n",mysql_error(mysql));
        return;
    }    
    // printf("获取结果集成功\n");
    
    MYSQL_ROW row;
    int i,j;
    for(i = 0;i<mysql_num_rows(pres);i++)
    {
        //读取结果集里边的一行数据
        row = mysql_fetch_row(pres);
        if(row ==  NULL)
        {
            printf("mysql_error:%s\n",mysql_error(mysql));
            return;
        }
 
        for(j=0;j<mysql_num_fields(pres);j++)
        {
            printf("%s\t",row[j]?row[j]:NULL);
        }
        printf("\n");
 
    }
 
 
    //释放结果集
    mysql_free_result(pres);
}
 
int search(MYSQL *mysql,Msg *msg)//查询数据表某一项
{
    int ret;
    char buf[256];
printf("nihaos\n");
    sprintf(buf,"SELECT * FROM %s WHERE account='%s'",TABLENAME,msg->account);
    ret = mysql_real_query(mysql,buf,strlen(buf));
    MYSQL_RES *pres = mysql_store_result(mysql);
    uint64_t rownum =  mysql_num_rows(pres);
    return rownum; 
}
 
 
int search1(MYSQL *mysql)//查询数据表
{
    int ret;
    char buf[256];
printf("nihaos\n");
    sprintf(buf,"SELECT * FROM %s ",TABLENAME);
    ret = mysql_real_query(mysql,buf,strlen(buf));
    MYSQL_RES *pres = mysql_store_result(mysql);
    uint64_t rownum =  mysql_num_rows(pres);
    return rownum; 
}
 
 
int data_insert(MYSQL *mysql,Msg *msg,int sockfd,int root)//插入数据表
{
    char query[1024];
    sprintf(query,"insert into %s values('%s','%s','%s','%d','%d','%d','%s')",
		TABLENAME ,msg->fromname,msg->account,msg->password,0,sockfd,root,msg->signname);
        printf("%s\n",query);
        if((mysql_real_query(mysql,query,(unsigned int)strlen(query))) != 0){
		
		printf("insert failed.\n");
		return 0;
	}
    return 1;
}
 
 
int  Entry_User(MYSQL *con,Msg *msg,int my_socket)//登录
{
    char query[1024],shi[1024];
    
    
    sprintf(query,"SELECT * FROM %s WHERE account = '%s'",TABLENAME,msg->account);
  
    if((mysql_real_query(con,query,(unsigned int)strlen(query))) != 0)
    {
		
		printf("insert failed.\n");
		return -1;
	}
    MYSQL_RES *pres = mysql_store_result(con);
    uint64_t rownum =  mysql_num_rows(pres);
    if(rownum==0)
    {
        return -2;
    }
    MYSQL_ROW row;
 
    int num_fields=mysql_num_fields(pres);
    row=mysql_fetch_row(pres);
    
    if(strcmp(row[3],"1")==0)
    {
        return -3;
    }
    
    if(strcmp(msg->password,row[2])!=0)
    {
        return -4;
    }
    char di[10];
    sprintf(di,"%d",my_socket);
    change(con,"socket",di,msg->account);
    change(con,"type","1",msg->account);
    
    strcat(msg->signname,row[6]);
    strcat(msg->fromname,row[0]);
    if(strcmp(row[5],"1")==0)
    msg->num=1;
    else
    msg->num=0;
    return 0;
    
 
}
 
int change(MYSQL *my_sql,char *field,char * changenaem,char *naem)//修改数据表
{
    char query[1024];
    sprintf(query,"update  %s set %s = '%s' where account='%s'",TABLENAME,field,changenaem,naem);
    if((mysql_real_query(my_sql,query,(unsigned int) strlen(query)))!=0)
    {
        printf("insert failed.\n");
		return -1;
    }
return 0;
 
}
 
 
int mysql_delete(MYSQL *my_sql,char *deletename)//删除数据表
{
    char query[1024];
    sprintf(query,"delete from %s  where account='%s'",TABLENAME,deletename);
    if((mysql_real_query(my_sql,query,(unsigned int) strlen(query)))!=0)
    {
        printf("insert failed.\n");
		return -1;
    }
return 0;
 
}
